<?php

/**
 * Compiles EuropaPHP together into one file. A single file creates a massive performance
 * increase thus eliminating the overhead associated with including files.
 */

ini_set('display_errors', 'on');
error_reporting(E_ALL ^ E_STRICT);

require './_Packages/FileSystem/Directory.php';
require './_Packages/FileSystem/File.php';

class Europa_Ultra
{
	private
		$_version,
		$_dir,
		$_compiled;
	
	
	
	public function __construct($version, $pathToEuropa = './trunk')
	{
		$this->_version = $version;
		$this->_dir     = new FileSystem_Directory($pathToEuropa);
	}
	
	public function __toString()
	{
		$contents = "<?php\n\n/**\n * EuropaPHP Ultra {$this->_version}\n */";
		
		foreach ($this->_dir->getChildren(null, '/\.txt$/i', null, true) as $file) {
			$lines = explode("\n", file_get_contents($file));
			
			foreach ($lines as &$line) {
				$line = ' * ' . trim($line);
			}
			
			$contents .= "\n\n\n\n/**\n" . implode("\n", $lines) . "\n */";
		}
		
		foreach ($this->_dir->getChildren(null, '/\.php$/i', null, true) as $file) {
			$temp = file_get_contents($file);
			$temp = preg_replace('/^\s*<\?php/', '', $temp);
			$temp = preg_replace('/\s*?>\s*$/', '', $temp);
			
			$contents .= "\n\n\n\n" . trim($temp);
		}
		
		$contents .= "\n";
		
		return $contents;
	}
	
	
	
	public function save($toFile)
	{
		file_put_contents($toFile, $this->__toString());
		
		return $this;
	}
}